package com.pda.xinma.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.SelectOrderAdapter;
import com.pda.xinma.bean.LatheOrderBean;
import com.wangshen.base.dialog.base.BaseDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectOrderDialog extends BaseDialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    SelectOrderAdapter selectOrderAdapter;
    OnSelectClick onSelectClick;
    public SelectOrderDialog(@NonNull Context context,OnSelectClick selectClick) {
        super(context);
        this.onSelectClick=selectClick;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

    }

    public void show( List<LatheOrderBean> list) {
        super.show();
        selectOrderAdapter=new SelectOrderAdapter(R.layout.item_machine_call,list);
        rvContent.setAdapter(selectOrderAdapter);
        selectOrderAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                for (LatheOrderBean latheOrderBean: selectOrderAdapter.getData()){
                    latheOrderBean.setSelect(false);
                }
                selectOrderAdapter.getData().get(position).setSelect(true);
                selectOrderAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getView() {
        return R.layout.dialog_select_order;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                if (selectOrderAdapter != null){
                    onSelectClick.onSelect(getSelect());
                    dismiss();
                }
                break;
        }
    }
    private LatheOrderBean getSelect(){
        LatheOrderBean selectBean = null;
        for (LatheOrderBean latheOrderBean: selectOrderAdapter.getData()){
            if (latheOrderBean.isSelect()){
                selectBean=latheOrderBean;
            }
        }
        return selectBean;
    }
    public  interface OnSelectClick{
        void onSelect(LatheOrderBean latheOrderBean);
    }
}
